package com.syezon.wifikey.msg.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hodanet.appadvplatclient.ad.AdInfo;
import com.syezon.wifikey.R;
import com.syezon.wifikey.imageloader.ImageLoader;
import com.syezon.wifikey.msg.info.MsgInfo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MsgAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private List<MsgInfo> mData = new ArrayList<>();
    private MsgItemClickListener mMsgItemClickListener;

    public MsgAdapter(Context context) {
        this.mContext = context;
    }

    public void setData(List<MsgInfo> data) {
        if (data != null) {
            mData.clear();
            mData.addAll(data);
            Collections.sort(mData);
            notifyDataSetChanged();
        }
    }

    public void setMsgItemClickListener(MsgItemClickListener listener) {
        this.mMsgItemClickListener = listener;
    }

    public void addData(MsgInfo data) {
        if (data != null) {
            mData.add(data);
            Collections.sort(mData);
            notifyDataSetChanged();
        }
    }

    public void addData(List<MsgInfo> data) {
        if (data != null) {
            mData.addAll(data);
            Collections.sort(mData);
            notifyDataSetChanged();
        }
    }

    public List<MsgInfo> getData() {
        return mData;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == MsgInfo.TYPE_ABOUT
                || viewType == MsgInfo.TYPE_FEED_BACK
                || viewType == MsgInfo.TYPE_AGREEMENT
                || viewType == MsgInfo.TYPE_PRIVACY
                || viewType == MsgInfo.TYPE_GRADE) {
            return new SettingViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_msg_setting, parent, false));
        }
        if (viewType == MsgInfo.TYPE_AD) {
            return new AdViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_msg_ad, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder != null) {
            MsgInfo msgInfo = mData.get(position);
            int type = msgInfo.getType();
            if ((type == MsgInfo.TYPE_ABOUT
                    || type == MsgInfo.TYPE_FEED_BACK
                    || type == MsgInfo.TYPE_AGREEMENT
                    || type == MsgInfo.TYPE_PRIVACY
                    || type == MsgInfo.TYPE_GRADE)
                    && holder instanceof SettingViewHolder) {
                SettingViewHolder viewHolder = (SettingViewHolder) holder;
                onBindSettingViewHolder(viewHolder, msgInfo);
            }
            if (type == MsgInfo.TYPE_AD && holder instanceof AdViewHolder) {
                AdViewHolder viewHolder = (AdViewHolder) holder;
                onBindAdViewHolder(viewHolder, msgInfo);
            }
        }
    }

    private void onBindAdViewHolder(final AdViewHolder viewHolder, final MsgInfo msgInfo) {
        final AdInfo adInfo = msgInfo.getAdInfo();
        if (adInfo != null) {
            ImageLoader.getInstance().displayImage(mContext, adInfo.getPic(), viewHolder.ivAd);
            if (!msgInfo.isRead()) {
                viewHolder.viewRedTip.setVisibility(View.VISIBLE);
            } else {
                viewHolder.viewRedTip.setVisibility(View.GONE);
            }
            viewHolder.tvTitle.setText(adInfo.getName());
            viewHolder.tvDes.setText(adInfo.getDesc());
            viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mMsgItemClickListener != null) {
                        viewHolder.viewRedTip.setVisibility(View.GONE);
                        mMsgItemClickListener.onAdClick(adInfo);
                    }
                }
            });
        }
    }

    private void onBindSettingViewHolder(SettingViewHolder viewHolder, final MsgInfo msgInfo) {
        viewHolder.tvGrale.setVisibility(View.GONE);
        if (msgInfo.getType() == MsgInfo.TYPE_GRADE) {
            viewHolder.tvGrale.setVisibility(View.VISIBLE);
        }
        viewHolder.ivIcon.setImageResource(msgInfo.getResId());
        viewHolder.tvTitle.setText(msgInfo.getTitle());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mMsgItemClickListener != null) {
                    mMsgItemClickListener.onSettingClick(msgInfo);
                }
            }
        });
    }

    @Override
    public int getItemViewType(int position) {
        MsgInfo msgInfo = mData.get(position);
        return msgInfo.getType();
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class SettingViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_icon)
        ImageView ivIcon;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_grade)
        TextView tvGrale;

        public SettingViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class AdViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_ad)
        ImageView ivAd;
        @BindView(R.id.view_red_tip)
        View viewRedTip;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.tv_dsc)
        TextView tvDes;

        public AdViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface MsgItemClickListener {
        void onSettingClick(MsgInfo msgInfo);

        void onAdClick(AdInfo adInfo);
    }
}
